package com.sprucetec.livechat.mcq.listener;

import com.alibaba.rocketmq.common.message.MessageExt;
import com.sprucetec.livechat.constants.MqConstant;
import com.sprucetec.livechat.mcq.process.NettyMessageProcess;
import com.sprucetec.mcq.consumer.MessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName NettyMessageListener
 * @Description:
 * @Author Kenny
 * @Date 2020/4/9
 **/

@Slf4j
@Component("nettyMessageListener")
public class NettyMessageListener implements MessageListener {
    @Value("${server.port}")
    private int port;
    @Autowired
    private NettyMessageProcess nettyMessageProcess;

    @Override
    public void onMessage(List<MessageExt> list) throws Exception {

        for (MessageExt message : list) {
            String messageBody = new String(message.getBody());
            log.info("MQ信息收到，TAG为=>{},收到了消息！！！", message.getTags());
            if (message.getTags().equals(MqConstant.ChatMq.TAG)) { // mq监听聊天信息
                nettyMessageProcess.processChatMessage(messageBody);
            } else if (message.getTags().equals(MqConstant.NettyMessageMq.TAG_NETTY_CHANNEL_REMOVE)) { // 移除channel 通道 游客离开
                log.info("移除channel通道");
                nettyMessageProcess.processChannelClose(messageBody);
            } else if (message.getTags().equals(MqConstant.NettyMessageMq.TAG_NETTY_VIDEO_CLOSE)) {// 直播平台关闭直播
                log.info("直播关闭");
                nettyMessageProcess.processCloseVideo(messageBody);
            } else if (MqConstant.VideoDataMq.TAG.equals(message.getTags())) { // 用户打开详情页 用户上线通知
                nettyMessageProcess.sendOnlineUser(messageBody);
            } else if (MqConstant.VideoDataMq.USER_AWAY.equals(message.getTags())) { // 用户下线通知
                nettyMessageProcess.sendOnlineUser(messageBody, true);
            }
        }
    }
}
